
const compressing = require('compressing')
const path = require('path')
const packageName = 'package.zip'
const fs = require('fs')

const pk = require('../package.json')
const libName = `tw-lib-${pk.name}`
// const libPackage = require(`../src/${libName}/package.json`)
const props = require(`../src/${libName}/props`)
const events = require(`../src/${libName}/events`)
let methods

try {
  methods = require(`../src/${libName}/methods`)
} catch (e) {
  methods = null
}

// libPackage.dependencies = pk.dependencies || {}
// fs.writeFileSync(path.resolve(`./src/${libName}/package.json`), JSON.stringify(libPackage, null, 2))

for (const prop of Object.values(props)) {
  if (typeof prop.default === 'function') {
    prop.default = prop.default()
  }
}

fs.writeFileSync(path.resolve(`./src/${libName}/props.json`), JSON.stringify({ props, events: events || [], methods }, null, 2))
// fs.copyFileSync(path.resolve('./package.json'), path.resolve('./src/package.json'))

compressing.zip.compressDir(path.resolve(`./src/${libName}`), packageName, {
  ignoreBase: true
}).then(() => {
  console.log('Success.')
}).catch(err => {
  console.error(err)
})
